<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--课程结构数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="conName"
            placeholder="请输入课程结构名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="stemCourseConOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--课程数据-->
      <el-col :span="20" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="120px"
        >
          <el-form-item label="课程名称" prop="courseName">
            <el-input
              v-model="queryParams.courseName"
              placeholder="请输入课程名称"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>

          <el-form-item label="课程难度等级" prop="difficultyLevel">
            <el-select
              v-model="queryParams.difficultyLevel"
              placeholder="请选择课程的难度等级"
              clearable
            >
              <el-option
                v-for="dict in dict.type.difficulty_level"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
              >搜索
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置
            </el-button>
          </el-form-item>
        </el-form>

        <div style="display: flex; flex-direction: row; flex-wrap: wrap">
          <div v-for="(item, index) in stemCourseList" :key="index">
            <el-card class="box-card" shadow="always">
              <div slot="header" class="clearfix">
                <span>{{ item.courseName }}</span>
                <el-button
                  style="float: right; margin: 0 3px"
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click.stop="onclick(item)"
                  v-hasPermi="['stemCourse:stemCourse:question']"
                  >查看
                </el-button>
              </div>
              <div class="text item">课程结构：{{ item.conName }}</div>
              <div class="text item">
                课程难度等级：{{ item.difficultyLevel }}
              </div>
              <div class="text item">
                适合学生年级：{{ item.recommendedGrade }}
              </div>
              <div class="text item">学分：{{ item.credits }}</div>
              <div class="text item">大致完成时间：{{ item.duration }}</div>
              <div class="text item">
                STEM学科：
                <dict-tag
                  style="display: inline-block"
                  :options="dict.type.subject_category"
                  :value="item.subjectCategory"
                />
              </div>
            </el-card>
          </div>
        </div>
        <el-empty
          v-if="stemCourseList.length == 0 || stemCourseList == null"
          description="暂无数据"
        ></el-empty>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {
  listStemCourse,
  getStemCourse,
  delStemCourse,
  addStemCourse,
  updateStemCourse,
  stemCourseConTreeSelect,
} from "@/api/stemCourse/stemCourse";
import { delStemCourseCon } from "@/api/stemCourseCon/stemCourseCon";
import Treeselect from "@riophae/vue-treeselect";
import { deptTreeSelect } from "@/api/system/user";

export default {
  name: "StemCourse",
  components: { Treeselect },
  dicts: ["difficulty_level", "subject_category"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // STEM课程表格数据
      stemCourseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 9,
        courseName: null,
        subjectCategory: null,
        difficultyLevel: null,
      },

      conName: undefined,
      stemCourseConOptions: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
    };
  },
  created() {
    this.getList();
    this.getStemCourseConTree();
  },
  methods: {
    // 查看课程详情
    onclick(item) {
      this.$router.push({
        path: "/stem/courseDetail",
        query: { courseId: item.courseId },
      });
    },
    /** 查询部门下拉树结构 */
    getStemCourseConTree() {
      stemCourseConTreeSelect().then((response) => {
        this.stemCourseConOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.conId = data.id;
      this.handleQuery();
    },
    delStemCourseCon,
    /** 查询STEM课程列表 */
    getList() {
      this.loading = true;
      listStemCourse(this.queryParams).then((response) => {
        this.stemCourseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>

<style scoped>
.box-card {
  width: 413px;
  margin: 16px 20px 0 0;
  cursor: pointer;
  border-radius: 15px;
}
</style>
